home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-02-07 | 3.4 KB | 126 lines | [TEXT/MPS ] |
- (*
- CTBRecvBytes([n]) -- Receive n characters, waiting until they're available if necessary.
- Default to all available characters. Return them as a list of numbers.
-
- To compile and link this file using Macintosh Programmer's Workshop,
-
- pascal -w CTBRecvBytes.p
- link -m ENTRYPOINT -o HyperCommands -rt XFCN=2759 -sn Main=CTBRecvBytes ∂
- CTBRecvBytes.p.o "{MPW}"Libraries:interface.o "{MPW}"Libraries:Libraries:HyperXLib.o
-
- © Copyright 1990 by Apple Computer, Inc.
-
- Initial coding 2/90 by Harry R. Chesley.
- *)
-
- {$R-}
-
- {$S CTBRecvBytes } { Segment name must be the same as the command name. }
-
- unit DummyUnit;
-
- interface
-
- uses MemTypes, QuickDraw, OSIntf, ToolIntf, CTBUtils, FTIntf, CMIntf, TMIntf, CRMIntf, HyperXCmd;
-
- procedure EntryPoint(paramPtr: XCmdPtr);
-
- implementation
-
- procedure CTBRecvBytes(paramPtr: XCmdPtr); forward;
-
- procedure EntryPoint(paramPtr: XCmdPtr);
-
- begin
- CTBRecvBytes(paramPtr);
- end;
-
- procedure CTBRecvBytes(paramPtr: XCmdPtr);
-
- {$I CTBUtil.inc}
-
- var toRead, haveRead: longInt;
- l: longInt;
- h: Handle;
- p: Ptr;
- theBuf: InputBufferHandle;
- sizes: CMBufferSizes;
- status: CMStatFlags;
- s: Str255;
-
- procedure Fail(errMsg: Str255); { set theResult and quit }
- begin
- paramPtr^.returnValue := PasToZero(paramPtr,errMsg);
- exit(CTBRecvBytes);
- end;
-
- begin
- { Check the number of parameters. }
- if paramPtr^.paramCount > 1 then Fail('Invalid parameter count');
-
- { Make sure the Comm Toolbox is ready. }
- CTBReady;
- { And there's a connection tool. }
- EnsurePresent(connectionTool);
- { And it's open. }
- EnsureOpen;
- { Get the input buffer. }
- theBuf := InputBufferHandle(CMGetUserData(Globals^^.connHand));
- { If there's any termination information around, get rid of it. }
- if theBuf^^.termString <> nil then
- begin
- DisposHandle(theBuf^^.termString);
- theBuf^^.termString := nil;
- theBuf^^.timeOut := -1;
- end;
-
- { Figure out how much to read. }
- if ParmPresent(1) then toRead := GetLongParm(1)
- else if CMStatus(Globals^^.connHand,sizes,status) = noErr then
- toRead := sizes[cmDataIn]+theBuf^^.amountLeft
- else toRead:= theBuf^^.amountLeft;
- { Allocate the space to read it into. }
- h := NewHandle(toRead);
- HLock(h);
- { Get the input. }
- haveRead := 0;
- if toRead > 0 then
- begin
- { Get what we can from the input buffer. }
- haveRead := min(toRead,theBuf^^.amountLeft);
- if haveRead > 0 then
- begin
- BlockMove(theBuf^^.bufferPtr,h^,haveRead);
- theBuf^^.amountLeft := theBuf^^.amountLeft - haveRead;
- toRead := toRead - haveRead;
- end;
- { Get the rest from outside (yes, even if it means suspending). }
- if toRead > 0 then haveRead := haveRead + ReadFromConn(Ptr(ord4(h^)+haveRead),toRead);
- end;
-
- { Convert the input to a string. }
- paramPtr^.returnValue := NewHandle(0);
- if paramPtr^.returnValue = nil then Fail('Out of memory');
- p := h^;
- { Convert each byte. }
- for l := 1 to haveRead do
- begin
- LongToStr(paramPtr,BAND(p^,$FF),s);
- s := Concat(s,',');
- if PtrAndHand(pointer(ord4(@s)+1),paramPtr^.returnValue,length(s)) <> noErr then
- begin
- DisposHandle(h);
- DisposHandle(paramPtr^.returnValue);
- Fail('PtrAndHand failed');
- end;
- p := pointer(ord4(p)+1);
- end;
- { Replace the last comma with a period (if there was a last comma). }
- p := pointer(ord4(paramPtr^.returnValue^)+GetHandleSize(paramPtr^.returnValue)-1);
- if p <> paramPtr^.returnValue^ then p^ := 0;
- { Dispose of the buffer. }
- DisposHandle(h);
- end;
-
- end.
-